package com.nanfang51g3.ckz.ui.accounting;


import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.AccountingBean;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.ui.base.BasePresenter;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class AccountingPresenter extends BasePresenter implements AccountingContract.AccountingActionsListener {

    String message;
    private final RestApi _restApi;
    private final AccountingContract.View mView;
    private String all_total;
    private String deal_total;

    @Inject
    public AccountingPresenter(RestApi restApi, AccountingContract.View mView) {
        KLog.i("TAG", "进入了...AccountingPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see AccountingPresenterModule#AccountingPresenterModule(AccountingContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void adList(int page, int limit) {
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("page", page + "");
        treeMap.put("limit", limit + "");
        treeMap.put("userid", userid);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.adList(page, limit, userid, sign);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new AccountingResultFunc<List<AccountingBean>>())
                .subscribe(new SubscriberInterface<List<AccountingBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        KLog.i("Evd", "onStart");
                        /**
                         * @see AccountingActivity#showProgress()
                         */
                        mView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");
                        /**
                         * @see AccountingActivity#dismissProgress()
                         */
                        mView.dismissProgress();

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see AccountingActivity#dismissProgress()
                         */
                        mView.dismissProgress();
                        /**
                         * @see AccountingActivity#showMessage(String)
                         */
                        mView.showMessage(msg);

                    }

                    @Override
                    public void onNext(List<AccountingBean> o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see AccountingActivity#showSuccess(List, String, String)
                         */
                        mView.showSuccess(o, all_total, deal_total);
                    }
                });


    }

    class AccountingResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            KLog.i("Evd", httpResult == null ? "1-" : "0" + httpResult.getAll_total() + "-" + httpResult.getDeal_total() );
            all_total = httpResult.getAll_total();
            deal_total = httpResult.getDeal_total();
            if (httpResult.getCode() == 2) {
                throw new Api2Exception("没有更多了");
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
